<?php
namespace app\erp\controller;

use app\erp\model\AccessConfigdb;
use app\erp\model\StaffAccessdb;

class AccessConfig extends Base
{

    protected $db;

    protected $cols = array(
        array(
            'field' => 'access_config_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'module',
            'width' => 160,
            'title' => '模块'
        ),
        array(
            'field' => 'controller_name',
            'width' => 160,
            'title' => '控制器'
        ),
        array(
            'field' => 'function_name',
            'width' => 160,
            'title' => '功能'
        ),
        array(
            'field' => 'use_intro',
            'width' => 160,
            'title' => '使用说明'
        ),
        array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#access_config_tool'
        )
    );

    protected $module_arr = array(
        '订单',
        '商品',
        '网站通用',
        '客户',
        '分站',
        '员工',
        '设置'
    );

    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        $this->db = new AccessConfigdb();
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        $this->assign('module_arr', $this->module_arr);
        return $this->fetch();
    }

    public function ajaxList()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSelect();
        $this->layuiReturn($return['data'], $return['count']);
    }

    public function tplForm()
    {
        $access_config_id = 0;
        $data = array();
        if (isset($this->post['access_config_id'])) {
            $this->db->param['access_config_id'] = $this->post['access_config_id'];
            $data = $this->db->ajaxFind();
            $site_config_id = $this->post['access_config_id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('access_config_id', $access_config_id);
        $this->assign('module_arr', $this->module_arr);
        return $this->fetch();
    }

    public function saveData()
    {
        $this->db->param = $this->post;
        $return = $this->db->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, - 1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $this->db->param = $this->post;
        $this->db->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }
    public function config(){
        $this->post['page'] = 1;
        $this->post['limit'] = 200;
        $this->db->param = $this->post;
        $data = $this->db->ajaxList();
        $this->assign('data',$data);
        $this->assign('staff_id',$this->post['staff_id']);
        $staffAccessdb = new StaffAccessdb();
        $staffAccessdb->param['staff_id'] = $this->post['staff_id'];
        $get_access = $staffAccessdb->getAccess();
        $this->assign('form_value',json_encode($get_access));
        return $this->fetch();
    }
    public function saveConfig(){
        $staffAccessdb = new StaffAccessdb();
        $staffAccessdb->param = $this->post;
        $data = $staffAccessdb->ajaxSave();
        $this->layuiReturn('保存成功',0);
    }
}